package com.good.shopping.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.good.shopping.product.entity.CategoryEntity;
import com.good.shopping.product.service.CategoryService;
import com.good.common.utils.PageUtils;
import com.good.common.utils.R;



/**
 * 商品三级分类
 *
 * @author yan
 * @email 821521880@qq.com
 * @date 2022-01-29 11:46:24
 */
@Api(value = "商品三级分类", tags = {"商品三级分类"})
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询所有分类，以树形结构查询
     */
    @GetMapping("/list/tree")
    @ApiOperation(value = "查询所有分类，以树形结构查询")
    //@RequiresPermissions("product:category:list")
    public R listTree(){
        List<CategoryEntity> categoryEntities = categoryService.listTree();
        return R.ok().put("data", categoryEntities);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:category:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    @ApiOperation(value = "查询信息")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);
        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);
        return R.ok();
    }


    /**
     * 批量修改
     */
    @PutMapping("/update/sort")
    @ApiOperation(value = "批量修改")
    //@RequiresPermissions("product:category:update")
    public R updateSort(@RequestBody CategoryEntity[] category){
        categoryService.updateBatchById(Arrays.asList(category));
        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
//		categoryService.updateById(category);
        categoryService.updateDetail(category);
        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds){
        // 真实删除
//		categoryService.removeByIds(Arrays.asList(catIds));
        // 逻辑删除
        categoryService.removeFakeByIds(Arrays.asList(catIds));
		return R.ok();
    }

}
